package com.zhai.cloud.test;




import com.zhai.cloud.common.util.StringUtil;
import com.zhai.cloud.home.HomeApplication;
import com.zhai.cloud.home.domain.JobIn;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URL;

/**
 * @author Created by 翟凤玺
 * @package com.zhai.cloud.test
 * @project-name zhai-cloud
 * @description 测试类
 * @create-datetime 2019-11-4 17:41
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes={HomeApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HomeTestCtr {

    private static final String BASE_URL = "http://localhost:8080/zhai-cloud-home";

    private static final String INTERFACE_JOB = "/job";

    private URL url;

    @Autowired
    private TestRestTemplate restTemplate;

    @Before
    public void setUrl() throws Exception {
        url = new URL(BASE_URL);
    }

    @Test
    public void testJob() throws Exception {
        for(int i = 500; i < 1500; i ++) {
            JobIn jobIn = new JobIn();
            jobIn.setCron("0 1 17 * * ? ");
            jobIn.setJobCode(String.valueOf(i));
            jobIn.setState("START");
            String urlStr = StringUtil.join(url.toString(), INTERFACE_JOB);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<JobIn> entity = new HttpEntity<>(jobIn, headers);
            ResponseEntity<Object> response = restTemplate.postForEntity(urlStr, entity, Object.class);
            System.out.println(String.format("测试结果为：%s", response.getBody()));
        }
    }

}
